package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.dto.AlarmInput;
import com.dabai.iot.core.dto.AlarmListenerInput;
import com.dabai.iot.core.dto.AlarmQuery;
import com.dabai.iot.core.entity.AlarmEntity;
import com.dabai.iot.core.entity.ProductEntity;
import com.dabai.iot.core.service.IAlarmService;
import com.dabai.iot.core.service.IProductService;
import com.dabai.iot.core.vo.AlarmVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 告警配置管理
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/alarm")
public class AlarmController extends BaseController {

    private final IAlarmService alarmService;
    private final IProductService productService;

    /**
     * 新增告警配置
     */
    @PostMapping("/add")
    @Operation(summary = "新增告警配置")
    public R<Void> add(@Valid @RequestBody AlarmInput alarmInput) {
        AlarmListenerInput alarmListener = alarmInput.getAlarmListener();
        if (alarmListener.getConditions().isEmpty()) {
            return R.fail("至少需要有一个监听条件");
        }

        ProductEntity product = productService.getProductCacheById(alarmInput.getProductId());
        if (product == null) {
            return R.fail("产品不存在");
        }

        alarmInput.setId(null);
        return toAjax(alarmService.addAlarm(alarmInput));
    }

    /**
     * 编辑告警配置
     */
    @PostMapping("/update")
    @Operation(summary = "编辑告警配置")
    public R<Void> update(@Valid @RequestBody AlarmInput alarmInput) {
        AlarmListenerInput alarmListener = alarmInput.getAlarmListener();
        if (alarmListener.getConditions().isEmpty()) {
            return R.fail("至少需要有一个监听条件");
        }

        ProductEntity product = productService.getProductCacheById(alarmInput.getProductId());
        if (product == null) {
            return R.fail("产品不存在");
        }

        return toAjax(alarmService.updateAlarm(alarmInput));
    }

    /**
     * 删除告警配置
     */
    @PostMapping("/remove")
    @Operation(summary = "删除告警配置", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        return toAjax(alarmService.removeAlarm(id));
    }

    /**
     * 告警配置详情
     */
    @GetMapping("/detail")
    @Operation(summary = "告警配置详情", description = "传入主键id")
    public R<AlarmVO> detail(@RequestParam Long id) {
        return R.ok(alarmService.getAlarmInfo(id));
    }

    /**
     * 告警配置分页
     */
    @GetMapping("/page")
    @Operation(summary = "告警配置分页", description = "传入对应查询参数")
    public TableDataInfo<AlarmEntity> page(PageQuery pageQuery, AlarmQuery alarmQuery) {
        IPage<AlarmEntity> pages = alarmService.getAlarmPage(pageQuery.build(), alarmQuery);
        return TableDataInfo.build(pages);
    }

}
